using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Drawing.Imaging;

namespace BingDownloader
{
    partial class SplashScreen : Form
    {
        System.Media.SoundPlayer _fadein = new System.Media.SoundPlayer(BingDownloader.Properties.Resources.Fadein);
        System.Media.SoundPlayer _fadeout = new System.Media.SoundPlayer(BingDownloader.Properties.Resources.Fadeout);

        public SplashScreen()
        {
            InitializeComponent();

            //  Initialize the AboutBox to display the product information from the assembly information.
            //  Change assembly information settings for your application through either:
            //  - Project->Properties->Application->Assembly Information
            //  - AssemblyInfo.cs            
            this.labelProductName.Text = String.Format("{0} {1} \r\n{2}", AssemblyTitle, AssemblyVersion, AssemblyCopyright);
            _fadein.Play();
            timer1.Start();

            string directoryName = Environment.CurrentDirectory + @"\BingImages\";
            DateTime fileDate = DateTime.Now.AddHours(-7).ToUniversalTime();
            string todayFile = fileDate.Day.ToString() + "-" +
                               fileDate.Month.ToString() + "-" +
                               fileDate.Year.ToString() + ".jpg";

            try
            {

                Directory.CreateDirectory(directoryName);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Cannot create BingImages folder, Try running BingDownloader from a location where you have write access. This is usually your Documents/Pictures folder rather than Program Files");
                return;
            }

            backgroundWorkerImageGetterUS.RunWorkerAsync(directoryName + "US_" + todayFile);
            backgroundWorkerImageGetterJP.RunWorkerAsync(directoryName + "JP_" + todayFile);
        }

        #region Assembly Attribute Accessors

        public string AssemblyTitle
        {
            get
            {
                // Get all Title attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                // If there is at least one Title attribute
                if (attributes.Length > 0)
                {
                    // Select the first one
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    // If it is not an empty string, return it
                    if (titleAttribute.Title != "")
                        return titleAttribute.Title;
                }
                // If there was no Title attribute, or if the Title attribute was the empty string, return the .exe name
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                // Get all Description attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                // If there aren't any Description attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Description attribute, return its value
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                // Get all Product attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                // If there aren't any Product attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Product attribute, return its value
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                // Get all Copyright attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                // If there aren't any Copyright attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Copyright attribute, return its value
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                // Get all Company attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                // If there aren't any Company attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Company attribute, return its value
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity > 0.8)
            {
                timer1.Stop();
            }
            else
            {
                this.Opacity += 0.1;
            }
        }
        protected override void WndProc(ref Message m)
        {
            const int WmNcHitTest = 0x84;
            const int WmNCDoubleClick = 0x00A3;
            const int HtClient = 1;
            const int HtCaption = 2;

            if (m.Msg == WmNCDoubleClick)
                m.Result = new IntPtr(HtClient);
            else if (m.Msg == WmNcHitTest)
                m.Result = new IntPtr(HtCaption);
            else
                base.WndProc(ref m);
        }
        private void AboutBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            _fadeout.Play();
            for (; this.Opacity > 0.1; )
            {
                this.Opacity -= 0.1;
                System.Threading.Thread.Sleep(25);
            }
        }

        private void buttonClose_MouseHover(object sender, EventArgs e)
        {
            buttonClose.Image = global::BingDownloader.Properties.Resources.closebright;
        }

        private void buttonClose_MouseLeave(object sender, EventArgs e)
        {
            buttonClose.Image = global::BingDownloader.Properties.Resources.close;
        }

        private void linkLabelemail1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:mugunth.kumar@gmail.com?subject=About BingDownloader");
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://mugunthkumar.com");
        }

        private void backgroundWorkerImageGetterUS_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                string strFileName = e.Argument as String;
                backgroundWorkerImageGetterUS.ReportProgress(20);
                string bingfilepath = BingHelper.GetBingUSImageFile();
                //20%
                backgroundWorkerImageGetterUS.ReportProgress(40);
                Bitmap bmpUS = BingHelper.RetriveBitmapAtLocation(bingfilepath);
                backgroundWorkerImageGetterUS.ReportProgress(80);
                //80%
                try
                {
                    bmpUS.Save(strFileName, ImageFormat.Jpeg);
                }
                catch (System.Runtime.InteropServices.ExternalException)
                {
                    MessageBox.Show("Some issue with the file name. :-(" + strFileName);
                }
                //100%

                e.Result = bmpUS;
                backgroundWorkerImageGetterUS.ReportProgress(100);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        private void backgroundWorkerImageGetterUS_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 100) progressBarUS.Visible = false;
            progressBarUS.Value = e.ProgressPercentage;
        }

        private void backgroundWorkerImageGetterUS_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bingPicUS.BackgroundImage = e.Result as Bitmap;
        }

        private void backgroundWorkerImageGetterJP_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorkerImageGetterJP.ReportProgress(20);
            string bingfilepath = BingHelper.GetBingInternationalImageFile();
            backgroundWorkerImageGetterJP.ReportProgress(40);
            Bitmap bmpInt = BingHelper.RetriveBitmapAtLocation(bingfilepath);
            backgroundWorkerImageGetterJP.ReportProgress(80);
            string strFileName = e.Argument as String;
            try
            {
                bmpInt.Save(strFileName, ImageFormat.Jpeg);
            }
            catch (System.Runtime.InteropServices.ExternalException)
            {
                MessageBox.Show("Some issue with the file name. :-(" + strFileName);
            }

            e.Result = bmpInt;
            backgroundWorkerImageGetterJP.ReportProgress(100);
        }

        private void backgroundWorkerImageGetterJP_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 100) progressBarJP.Visible = false;
            progressBarJP.Value = e.ProgressPercentage;
        }

        private void backgroundWorkerImageGetterJP_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bingPicJP.BackgroundImage = e.Result as Bitmap;
        }

        private void bingPicUS_Click(object sender, EventArgs e)
        {

        }

        private void bingPicJP_Click(object sender, EventArgs e)
        {

        }
    }
}
